package com.store.app.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.adapter.SearchOrderItemAdapter;
import com.store.app.bean.SearchOrderBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/29.
 * 根据提货码搜索订单的页面
 */
public class SearchOrderActivity extends BaseActivity implements IOnHttpListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private ListView lv;
    private List<SearchOrderBean> searchOrderBeanList;
    private SearchOrderItemAdapter adapter;
    private NetRequest netRequest;
    private TextView tv_no_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_order);
        initView();
        String loaded_code = getIntent().getStringExtra("loaded_code");
        netRequest = new NetRequest(this);
        netRequest.requestByloadedCodeForStoresFind(1, loaded_code);
        showProgressDialog("", "正在加载数据...");
    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        showToast("请检查本地网络!");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            Gson gson = new Gson();
            searchOrderBeanList.clear();
            try {
                JSONObject jsonObject = new JSONObject(result);
                JSONArray jsonArray = jsonObject.getJSONArray("list");
                for (int i = 0; i < jsonArray.length(); i++) {
                    SearchOrderBean searchOrderBean = gson.fromJson(jsonArray.getJSONObject(i).toString(), SearchOrderBean.class);
                    searchOrderBeanList.add(searchOrderBean);
                }

                if(searchOrderBeanList.size()==0){
                    tv_no_data.setVisibility(View.VISIBLE);
                }else{
                    tv_no_data.setVisibility(View.GONE);
                }
                adapter.notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        showToast(msg);
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("卖单");
        lv = (ListView) findViewById(R.id.lv);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        searchOrderBeanList = new ArrayList<>();
        adapter = new SearchOrderItemAdapter(this, searchOrderBeanList);
        lv.setAdapter(adapter);
        tv_no_data = (TextView) findViewById(R.id.tv_no_data);
    }
}
